/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.view;

import entity.Jobs;
import entity.Message;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import session.JobsFacade;
import utility.Page;

/**
 *
 * @author owtee
 */
public class ApplicationMsgGetAll extends HttpServlet {

    @EJB
    private JobsFacade jobsFacade;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            try {
                String txtReqDateStKey = (request.getParameter("txtReqDateStKey") != null
                        ? request.getParameter("txtReqDateStKey")
                        : "");
                String txtReqDateEnKey = (request.getParameter("txtReqDateEnKey") != null
                        ? request.getParameter("txtReqDateEnKey")
                        : "");
                String txtExpDateStKey = (request.getParameter("txtExpDateStKey") != null
                        ? request.getParameter("txtExpDateStKey")
                        : "");
                String txtExpDateEnKey = (request.getParameter("txtExpDateEnKey") != null
                        ? request.getParameter("txtExpDateEnKey")
                        : "");
                String txtJobIdKey = (request.getParameter("txtJobIdKey") != null
                        ? request.getParameter("txtJobIdKey")
                        : "");

                int pageNo = (request.getParameter("page_no") != null
                        && request.getParameter("page_no").length() > 0
                        ? Integer.parseInt(request.getParameter("page_no"))
                        : 0);

                ServletContext context = getServletContext();
                int ITEM_PER_PAGE = Integer.parseInt(context.getInitParameter("ITEM_PER_PAGE"));

                // Set status
                List<String> sStatusId = new ArrayList<String>();
                sStatusId.add("01");
                sStatusId.add("02");
                // Set date                
                SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
                Date dReqDateStKey = new Date();
                Date dReqDateEnKey = new Date();
                Date dExpDateStKey = new Date();
                Date dExpDateEnKey = new Date();

                if (txtReqDateStKey.equals("") || txtReqDateEnKey.equals("")) {
                    Calendar calendar = Calendar.getInstance();
                    // Clear time
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
                    // Set -1 and +1 for day
                    calendar.add(Calendar.DATE, -1);
                    dReqDateStKey = calendar.getTime();
                    calendar.add(Calendar.DATE, 2);
                    dReqDateEnKey = calendar.getTime();

                    txtReqDateStKey = dateFormat.format(dReqDateStKey);
                    txtReqDateEnKey = dateFormat.format(dReqDateEnKey);
                } else {
                    dReqDateStKey = dateFormat.parse(txtReqDateStKey);
                    dReqDateEnKey = dateFormat.parse(txtReqDateEnKey);
                }

                if (txtExpDateStKey.equals("") || txtExpDateEnKey.equals("")) {
                    Calendar calendar = Calendar.getInstance();
                    // Clear time
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
                    // Set -1 and +1 for day
                    calendar.add(Calendar.DATE, -1);
                    dExpDateStKey = calendar.getTime();
                    calendar.add(Calendar.DATE, 2);
                    dExpDateEnKey = calendar.getTime();

                    txtExpDateStKey = dateFormat.format(dExpDateStKey);
                    txtExpDateEnKey = dateFormat.format(dExpDateEnKey);
                } else {
                    dExpDateStKey = dateFormat.parse(txtExpDateStKey);
                    dExpDateEnKey = dateFormat.parse(txtExpDateEnKey);
                }

                List<Jobs> jobLstTmp = jobsFacade.findMsgAll(true,
                        sStatusId,
                        "%" + txtJobIdKey + "%",
                        dReqDateStKey,
                        dReqDateEnKey,
                        dExpDateStKey,
                        dExpDateEnKey);
                int numRecord = jobLstTmp.size();
                Page page = new Page(numRecord, pageNo, ITEM_PER_PAGE);

                List<Jobs> jobLst = jobsFacade.findMsgAll(page.getFromIndex(),
                        ITEM_PER_PAGE,
                        true,
                        sStatusId,
                        "%" + txtJobIdKey + "%",
                        dReqDateStKey,
                        dReqDateEnKey,
                        dExpDateStKey,
                        dExpDateEnKey);
                int pageCount = page.size();

                request.setAttribute("jobLst", jobLst);
                request.setAttribute("pageCount", pageCount);
                request.setAttribute("pageNo", pageNo);
                request.setAttribute("txtReqDateStKey", txtReqDateStKey);
                request.setAttribute("txtReqDateEnKey", txtReqDateEnKey);
                request.setAttribute("txtExpDateStKey", txtExpDateStKey);
                request.setAttribute("txtExpDateEnKey", txtExpDateEnKey);
                request.setAttribute("txtJobIdKey", txtJobIdKey);

            } catch (Exception ex) {
                Message message = new Message();
                message.setMesDetail(ex.getMessage());
                request.setAttribute("message", message);
            }

            RequestDispatcher rd = request.getRequestDispatcher("application_msg_all.jsp");
            rd.forward(request, response);

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
